package net.assassin8.bautista.stalker;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SelectMallActivity extends Activity {

	private final Context ctx = this;

	@SuppressWarnings("unused")
	private static final Stalker stalker = new Stalker();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initSelectMall();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.select_mall_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.menuAddMall:
			// Create an intent to go to MallDownloadActivity
			Intent myIntent = new Intent(SelectMallActivity.this,
					MallDownloadActivity.class);
			myIntent.putExtra("test", true);
			SelectMallActivity.this.startActivity(myIntent);
			SelectMallActivity.this.finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void initSelectMall() {
		// Set the layout
		setContentView(R.layout.select_mall);

		// Grab the linear layout
		LinearLayout layout = (LinearLayout) findViewById(R.id.SelectMallLayout);

		// Grab malls from DB

		StalkerProvider db = new StalkerProvider(ctx);

		db.open();

		Cursor malls = db.getAllMalls();

		if (malls.moveToFirst()) {
			// Create buttons for each mall
			Button[] mallButtons = new Button[malls.getCount()];

			for (int i = 0; i < malls.getCount(); i++) {
				malls.moveToPosition(i);
				
				// Create the button for this mall
				mallButtons[i] = new Button(this);
				mallButtons[i].setText(malls.getString(1));
				
				final long mallId = malls.getLong(0);
				final String mallName = malls.getString(1);
				final String mallAddress = malls.getString(2);
				final String mallDescription = malls.getString(3);
				final int mallFloors = malls.getInt(4);
				
				mallButtons[i].setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View arg0) {
						Intent i = new Intent(SelectMallActivity.this, MallDetailsActivity.class);
						i.putExtra("MALL_ID", mallId);
						i.putExtra("MALL_NAME", mallName);
						i.putExtra("MALL_ADDRESS", mallAddress);
						i.putExtra("MALL_DESCRIPTION", mallDescription);
						i.putExtra("MALL_FLOORS", mallFloors);
						SelectMallActivity.this.startActivity(i);
						SelectMallActivity.this.finish();
					}
					
				});
				
				//stalker.alert(ctx, malls.getString(1));
				
				// Add the button to the parent layout
				layout.addView(mallButtons[i], new LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			}
		} else {
			// Just show a textbox with a tip on how to add a mall
			TextView tip = new TextView(this);
			tip.setTextSize(20);
			tip.setText("No malls have been added yet. To add a mall, press the menu button and click \"Add a Mall\".");
			layout.addView(tip);
		}

		// Close the mall cursor
		malls.close();

		db.close();

	}

}